<?php
class ccdsProject
	extends ccdsBackendPlugin
{
	public $name = "ccdsProject";

	/* ---------------------------------------------------------------------------------------------------------------------------------- */

	function __construct()
	{
		parent::__construct();

		$this->xajax = ccdsXajax::getInstance();

		$this->xajax->register( XAJAX_CCDS_FUNCTION, "ProjectNew", $this->name );
		$this->xajax->register( XAJAX_CCDS_FUNCTION, "ProjectNewSave", $this->name );
		$this->xajax->register( XAJAX_CCDS_FUNCTION, "ProjectEdit", $this->name );
		$this->xajax->register( XAJAX_CCDS_FUNCTION, "ProjectEditSave", $this->name );
		$this->xajax->register( XAJAX_CCDS_FUNCTION, "ProjectDomains", $this->name );
		$this->xajax->register( XAJAX_CCDS_FUNCTION, "ProjectDomainEdit", $this->name );

		$this->xajax->register( XAJAX_CCDS_FUNCTION, "ProjectDomainNewSave", $this->name );
		$this->xajax->register( XAJAX_CCDS_FUNCTION, "ProjectDomainEditSave", $this->name );
		$this->xajax->register( XAJAX_CCDS_FUNCTION, "ProjectRootAdd", $this->name );

		$this->xajax->register( XAJAX_CCDS_FUNCTION, "ProjectNewConfirm", $this->name );
	}
	/* ---------------------------------------------------------------------------------------------------------------------------------- */

	function getAdminNavi($elements) {

		if (($_SESSION['CMS']['user_level'] >= 1000) OR ($_SESSION['CMS']['user_flags']['project_new']))
		{
			$entry = array(
				'icon' => $this->config->get('LIBRARY_WEB').'famfamfam/icons/folder_add.png',
				'onclick' => "xajax.request({ccdsfun:'ProjectNew',ccdsclass:'ccdsProject'},{parameters:[{}]});",
				'label' => $this->smarty->get_config_vars('Global_new_project')
			);		
			$elements[0][] = $entry;
			return true;
		}
		return false;
	}
	/* ---------------------------------------------------------------------------------------------------------------------------------- */

	Public function ProjectNew( $aFormValues )
	{
		$objResponse = new ccdsResponse();

		$extension_folders = $this->tools->getFileListing( $this->config->get( 'EXTENSION_DIR' ), 'repository/' );
		$extension = array();
		foreach ( $extension_folders as $key => $ext )
		{
			if ( ($ext['filetype'] != "goup") && ($ext['name'] != "ccdsModules") )
			{
				$class_files = $this->tools->getFileListing( $this->config->get( 'EXTENSION_DIR' ), 'repository/'.$ext['name']."/" );

				foreach ( $class_files as $class_id => $class )
				{
					if ( $class['filetype'] != "goup"  )
					{
						$filename = $this->config->get( 'EXTENSION_DIR' ).'repository/'.$ext['name']."/".$class['name']."/install.conf";

						if ( file_exists( $filename ) )
						{

							$ini = new ccdsXMLparser( simplexml_load_file($filename) );
							$config = $ini->getArray();
							
							if (!isset($extensions[$key])) 
							{
								$extensions[$key] = $ext;
								$extensions[$key]['classes'] = array();
							}
							if (!isset($extensions[$key]['classes'][$class_id]))
							{
								$extensions[$key]['classes'][$class_id]=$class;
							}
							$extensions[$key]['classes'][$class_id]['info'] = $config['config']['info'];
						}
					}
				}
			}
		}

		$objResponse->loadcommands( $this->tools->addTabManager( 'topTab', "ProjectNew", "<img src=\"".$icon_src."\" border=\"0\" align=\"left\" valign=\"top\" /><span style=\"margin-left:4px;\">neues Projekt anlegen</span>" ) );

		$hide_panel = array
		(
			"list" => true,
			"overview" => true,
			"edit" => false,
			"parameter" => true
		);

		/* new overview panel */
		$objResponse->clsTabManager->addPanel( "TabManagerContent_ProjectNew", array
		(
			"id" => "DivProjectNew".$obj['obj_id'],
			"title" => "<img src=\"".$this->config->get( 'CMS_WEB_DIR' )."img/icon-preferences.gif\" style=\"width:16px\" border=\"0\" align=\"left\" valign=\"top\" /><span style=\"margin-left:4px;\">".$this->labels['Project']."</span>",
			"content" => "<div id='divProjectNewContentArea'></div>",
			"closeable" => false
		));

		$this->smarty->assign( "extensions", $extensions );
		$sHtml = $this->smarty->fetch( $this->tpl_dir."project_new.tpl" );
		$objResponse->assign( "divProjectNewContentArea", "innerHTML", $sHtml );

		return $objResponse;
	}


	/* ---------------------------------------------------------------------------------------------------------------------------------- */

	function ProjectNewConfirm( $aFormValues )
	{

		$objResponse = new ccdsResponse();
		$mysql_config = $this->config->get( 'mysql' );

		if ( '' == $aFormValues['project_name'] )
		{
			$objResponse->assign( "project_new_error", "innerHTML", "Bitte geben Sie einen Namen ein!" );
			return $objResponse;
		}

		$this->smarty->assign("project_name",$aFormValues['project_name']);

		$query = "SELECT * FROM projects where project_name='".$this->ccdSQL->real_escape_string( $aFormValues['project_name'] )."'";
		$res = $this->ccdSQL->query( $query );

		if ( $row = $this->ccdSQL->fetch_assoc( $res ) )
		{
			$objResponse->assign( "project_new_error", "innerHTML", $this->labels['Project_already_assigned'] );
			return $objResponse;
		}
		$classes = array ();
		
		if (is_array($aFormValues['classes']))
		foreach ( $aFormValues['classes'] as $class_dir )
		{
			$class_conf = array ();
			list( $ext, $class ) = split( '/', $class_dir );

			$class_conf['class_name'] = $class;
			$class_conf['ext_name'] = $ext;

			$filename = $this->config->get( 'EXTENSION_DIR' ).'repository/'.$class_dir."/config/".$class.".conf";

			if ( file_exists( $filename ) )
			{
				
				
				$ini = new ccdsXMLparser( simplexml_load_file($filename) );
				$config = $ini->getArray();
				if ( is_array( $config['data'] ) )
				{
					if ( $config['data']['name'] != "" )
					{
						$class_conf['tbl_name'] = $config['data']['name'];
						$query = " SHOW TABLE STATUS FROM ".$mysql_config['servers']['slave']['db']." WHERE name = '".$config['data']['name']."' ";
						$res = $this->ccdSQL->query( $query );

						if ( $row = $this->ccdSQL->fetch_assoc( $res ) )
						{
							$class_conf['table_exists'] = true;
						}
					}
				}
			}
			$classes[] = $class_conf;
		}

		$this->smarty->assign( "classes", $classes );

		$sHtml = $this->smarty->fetch( $this->tpl_dir."project_new_confirm.tpl" );
		$objResponse->assign( "divProjectNewContentArea", "innerHTML", $sHtml );

		return $objResponse;
	}

	/* ---------------------------------------------------------------------------------------------------------------------------------- */

	function ProjectNewSave( $aFormValues )
	{

		$objResponse = new ccdsResponse();
		$project_name = $aFormValues['project_name'];
		$project_dir = $this->tools->rewrite_name( $project_name );
		$i = 0;

		$mysql_config = $this->config->get( 'mysql' );
		$db_name = $mysql_config['servers']['slave']['db'];
		
		if (is_array($aFormValues['tbl_name'])) 
		{
			$error = false;
			foreach ($aFormValues['tbl_name'] as $class => $value) {

				$query = " SHOW TABLE STATUS FROM ".$db_name." WHERE name = '".$value."' ";
				$res = $this->ccdSQL->query( $query );
				if ( $row = $this->ccdSQL->fetch_assoc( $res ) )
				{
					$error=true;
					$objResponse->assign("project_new_cls_".$class,"innerHTML",$this->labels['Project_table_already_assigned']);
				}
			}
			if ($error) return $objResponse;
		}


		while ( is_dir( $this->config->get( 'EXTENSION_DIR' ).$project_dir."/" ) )
		{
			$project_dir = $this->tools->rewrite_name( $project_name ).$i;
			$i++;
		}

		$query = "INSERT INTO projects (project_name,project_dir,project_domains)
	           VALUES (
	           '".addslashes( $aFormValues['project_name'] )."',
	           '".addslashes( $project_dir )."',
	           '".addslashes( $aFormValues['project_domains'] )."'
	           ) 
	           ";
		$this->ccdSQL->query( $query );
		$project_id = $this->ccdSQL->insert_id();

		$this->tools->mkdir($this->config->get( 'SMARTY_COMPILE_DIR' ).$project_dir);
		$this->tools->mkdir($this->config->get( 'SMARTY_CACHE_DIR' ).$project_dir);
		$this->tools->mkdir($this->config->get( 'MEDIA_DIR' ).'/'.$project_dir);
		$this->tools->mkdir($this->config->get( 'EXTENSION_DIR' ).'/'.$project_dir);

		$noncache = $this->config->get('EXTENSION_DIR').$project_dir.'/noncache.inc.php';
		file_put_contents($noncache,'');
		chmod($noncache,$this->config->get('FILE_CHMOD'));

		if ( is_array( $aFormValues['class'] ) )
			foreach ( $aFormValues['class'] as $class )
			{

				list( $ext_dir, $class_dir ) = split( "/", $class );

				$query = "SELECT * FROM extensions WHERE ext_key_project_id='".$project_id."' AND ext_name='".$ext_dir."'";
				$res = $this->ccdSQL->query( $query );

				if ( $extension = $this->ccdSQL->fetch_assoc( $res ) )
				{
					$extension_id = $extension['ext_id'];
				}
				else
				{
					$this->ccdSQL->query( "INSERT INTO extensions (ext_name,ext_dir,ext_key_project_id)
	                      VALUES (
	                      '".$ext_dir."',
	                      '".$ext_dir."',
	                      '".$project_id."'
	                      )
	                      " );
					$extension_id = $this->ccdSQL->insert_id();
				}
				$query = "INSERT INTO classes (class_name,class_dir,class_key_ext_id)
	               VALUES (
	               '".strtoupper( $class_dir )."',
	               '".$class_dir."',
	               '".$extension_id."'
	               )
	               ";
				$this->ccdSQL->query( $query );
				$class_id = $this->ccdSQL->insert_id();

				$path = $this->config->get( 'EXTENSION_DIR' ).$project_dir."/".$ext_dir;

				if ( !is_dir( $path ) )
					@mkdir( $path, 0755, true );
				$path = $this->config->get( 'EXTENSION_DIR' ).$project_dir."/".$ext_dir."/".$class_dir;

				if ( !is_dir( $path ) )
					@mkdir( $path, 0755, true );

				$filename = $this->config->get( 'EXTENSION_DIR' ).'repository/'.$class."/config/".$class_dir.".conf";
				//$response = $this->create_table_from_conf( $filename, $class_dir );
				$this->tools->full_copy( $this->config->get( 'EXTENSION_DIR' ).'repository/'.$class."/", $this->config->get( 'EXTENSION_DIR' ).$project_dir.'/'.$class."/" );
			}

		if ( is_array( $aFormValues['class'] ) )
			foreach ( $aFormValues['class'] as $class )
			{

				list( $ext_dir, $class_dir ) = split( "/", $class );
				$filename = $this->config->get( 'EXTENSION_DIR' ).'repository/'.$class."/install.conf";

				$query = "SELECT * FROM classes LEFT JOIN extensions ON class_key_ext_id=ext_id WHERE class_name='".$class_dir."' AND ext_key_project_id='".$project_id."'";
				$res = $this->ccdSQL->query( $query );
				$current_class = $this->ccdSQL->fetch_assoc( $res );

				if ( file_exists( $filename ) )
				{

					$xml = new ccdsXMLparser( simplexml_load_file($filename) );
					$config = $xml->getArray();
					
					if ( is_array( $config['dependency'] ) )
						foreach ( $config['dependency'] as $dep_class ) {
					}
					if ( is_array( $config['permissions'] ) )
						foreach ( $config['permissions'] as $node_class )
						{
							$query = "SELECT * FROM classes LEFT JOIN extensions ON class_key_ext_id=ext_id WHERE class_name='".$node_class."' AND ext_key_project_id='".$project_id."'";
							$res = $this->ccdSQL->query( $query );
							$node = $this->ccdSQL->fetch_assoc( $res );

							if ( ( $node['class_id'] != "" ) AND ( $current_class['class_id'] != "" ) )
							{
								$query = "INSERT INTO permissions_class_2_class (class_2_class_node_id,class_2_class_child_id) VALUES('".$node['class_id']."','".$current_class['class_id']."')";
								$this->ccdSQL->query( $query );
							}
						}

					if ( is_array( $config['templates'] ) ) 
					{
						foreach ( $config['templates'] as $tpl_name => $tpl )
						{
							if ( $tpl['default'] == "true" )
							{
								$default = 1;
							}
							else
							{
								$default = 0;
							}
							$query = "INSERT INTO templates (tpl_name,tpl_file,tpl_dir,tpl_key_project_id)
												VALUES (
												'".$tpl_name."',
												'".$tpl['file']."',
												'".$project_dir."/".$ext_dir."/".$class_dir."/tpl/',
												'".$project_id."'
												)
												";
							$this->ccdSQL->query( $query );
							$tpl_id = $this->ccdSQL->insert_id();
							$query = "INSERT INTO mn_template_class
												(
													tpl_class_key_tpl_id,
													tpl_class_key_class_id,
													tpl_class_default
												)
												VALUES (
													'".$tpl_id."',
													'".$current_class['class_id']."',
													'".$default."'
												)
											";
							$this->ccdSQL->query($query);
						}
					}
				}


				$filename = $this->config->get( 'EXTENSION_DIR' ).'repository/'.$class."/config/".$class_dir.".conf";

	
				if ( file_exists( $filename ) )
				{

					$xml = new ccdsXMLparser( simplexml_load_file($filename) );
					$config = $xml->getArray();
						
					if ( is_array( $config['data'] ) )
					{
						if ( $aFormValues['tbl_name'][$class_dir] != "" )
						{
							$config['data']['name'] = $aFormValues['tbl_name'][$class_dir];
							$foo = new ccdsTableCreator( $config );
							$foo->create();
						} else {
							//$objResponse->alert("no table name defined");
						}
					}
				} 
			}
		$objResponse->loadClassCommand( "ccdsDefaultPlugin", "initNavi" );
		$objResponse->clsTabManager->close( 'topTab', 'ProjectNew' );
		$objResponse->loadcommands( $this->ProjectEdit( array ("project_id" => $project_id)) );
		return $objResponse;
	}

	/* ---------------------------------------------------------------------------------------------------------------------------------- */

	function ProjectEdit( $aFormValues )
	{

		$objResponse = new ccdsResponse();

		$extensions = $this->tools->getFileListing( $this->config->get( 'EXTENSION_DIR' ), 'repository/' );

		$query = "SELECT * FROM projects
	           LEFT OUTER JOIN permissions_project_user
	           ON project_id = project_user_key_project_id
	           WHERE project_id='".$aFormValues['project_id']."'
	           ";
		$res = $this->ccdSQL->query( $query );
		$project = $this->ccdSQL->fetch_assoc( $res );
		$this->smarty->assign( "project", $project );

		$lifetime = array ();
		$lifetime['min'] = array ();

		for ( $i = 0; $i <= 59; $i++ )
		{
			$lifetime['min'][] = $i;
		}
		$lifetime['hour'] = array ();

		for ( $i = 0; $i <= 23; $i++ )
		{
			$lifetime['hour'][] = $i;
		}
		$lifetime['days'] = array ();

		for ( $i = 0; $i <= 2; $i++ )
		{
			$lifetime['days'][] = $i;
		}

		$this->smarty->assign( "lifetime", $lifetime );

		foreach ( $extensions as $key => $ext )
		{
			if ( $ext['filetype'] != "goup" )
			{
				$extensions[$key]['classes'] = $this->tools->getFileListing( $this->config->get( 'EXTENSION_DIR' ), 'repository/'.$ext['filename']."/" );

				foreach ( $extensions[$key]['classes'] as $class_id => $class )
				{
					if ( $class['filetype'] != "goup" )
					{
						$filename = $this->config->get( 'EXTENSION_DIR' ).'repository/'.$ext['filename']."/".$class['filename']."/install.conf";

						if ( file_exists( $filename ) )
						{
							$xml = new ccdsXMLparser( $filename );
							$config = $xml->getArray();
							$extensions[$key]['classes'][$class_id]['info'] = $config['config']['info'];
						}
					}
				}
			}
		}
		$this->smarty->assign( "extensions", $extensions );
		$navitree = $this->tools->getNaviTree( 0, "", $aFormValues['project_id'] );
		$this->smarty->assign( "navitree", $navitree );

		$languages = array ();
		$query = "SELECT * FROM languages LEFT OUTER JOIN
	           mn_project_language
	           ON (project_language_key_language_id=lang_id AND project_language_key_project_id='".$aFormValues['project_id']."')
	           WHERE lang_active=1
	           order by lang_name";
		$res = $this->ccdSQL->query( $query );

		while ( $row = $this->ccdSQL->fetch_assoc( $res ) )
			$languages[] = $row;
		$this->smarty->assign( "languages", $languages );


		$obj_key_class_id = $obj['obj_key_class_id'];

		$query = "SELECT
							*
							FROM
							classes LEFT JOIN extensions ON class_key_ext_id=ext_id
							WHERE
							ext_key_project_id='".$project['project_id']."'
							ORDER BY
							class_name
           ";

		$res = $this->ccdSQL->query( $query );
		$classes = array ();

		while ( $row = $this->ccdSQL->fetch_assoc( $res ) )
		{
			$classes[] = $row;
		}
		$this->ccdSQL->free_result( $res );
		$this->smarty->assign( "classes", $classes );


		$sHtml = $this->smarty->fetch( $this->tpl_dir."project_edit.tpl" );

		$objResponse->loadCommands( $this->tools->addTabManager( 'topTab', 'ProjectEdit'.$project['project_id'], 'Projekt: '.$project['project_name'] ) );
		$objResponse->clsTabManager->addPanel( 'TabManagerContent_ProjectEdit'.$project['project_id'], array
		(
			"id" => "ProjectEditContainer".$project['project_id'],
			"title" => $this->labels['Project_overview'],
			"content" => "<div id='ProjectEditDiv".$project['project_id']."'></div>",
			"closeable" => false
		));

		$objResponse->clsTabManager->addPanel( 'TabManagerContent_ProjectEdit'.$project['project_id'], array
		(
			"id" => "ProjectDomainsContainer".$project['project_id'],
			"title" => $this->labels['Project_domains'],
			"content" => "<div id='ProjectDomainDiv".$project['project_id']."'></div>",
			"closeable" => false,
			"hide" => true
		));





		$objResponse->assign( "ProjectEditDiv".$project['project_id'], "innerHTML", $sHtml );
		$objResponse->loadcommands( $this->ProjectDomains( $aFormValues ) );
		return $objResponse;
	}

	/* ---------------------------------------------------------------------------------------------------------------------------------- */
	function ProjectDomains( $aFormValues )
	{

		$objResponse = new ccdsResponse();

		$extensions = $this->tools->getFileListing( $this->config->get( 'EXTENSION_DIR' ), 'repository/' );

		$query = "SELECT * FROM projects
	           LEFT OUTER JOIN permissions_project_user
	           ON project_id = project_user_key_project_id
	           WHERE project_id='".$aFormValues['project_id']."'
	           ";
		$res = $this->ccdSQL->query( $query );
		$project = $this->ccdSQL->fetch_assoc( $res );
		$this->smarty->assign( "project", $project );

		$navitree = $this->tools->getNaviTree( 0, "", $aFormValues['project_id'] );
		$this->smarty->assign( "navitree", $navitree );

		$languages = array ();
		$query = "SELECT * FROM languages LEFT OUTER JOIN
	           mn_project_language
	           ON (project_language_key_language_id=lang_id AND project_language_key_project_id='".$aFormValues['project_id']."')
	           WHERE lang_active=1
	           order by lang_name";
		$res = $this->ccdSQL->query( $query );

		while ( $row = $this->ccdSQL->fetch_assoc( $res ) )
			$languages[] = $row;
		$this->smarty->assign( "languages", $languages );

		$project_domains = array ();
		$query = "SELECT * FROM project_domains WHERE project_domain_key_project_id='".$aFormValues['project_id']."'";
		$res = $this->ccdSQL->query( $query );

		while ( $row = $this->ccdSQL->fetch_assoc( $res ) )
		{
			$row['PATH'] = $this->tools->getRecursivePath( $row['project_domain_key_obj_id'], $_SESSION['CMS']['lang'][$aFormValues['project_id']], true );
			$project_domains[] = $row;
		}
		$this->smarty->assign( "project_domains", $project_domains );
		$sHtml = $this->smarty->fetch( $this->tpl_dir."project_domains.tpl" );
		$objResponse->assign( "ProjectDomainDiv".$aFormValues['project_id'], "innerHTML", $sHtml );

		return $objResponse;
	}

	/* ---------------------------------------------------------------------------------------------------------------------------------- */
	function ProjectDomainEdit( $aFormValues )
	{

		$objResponse = new ccdsResponse();

		$query = "SELECT * FROM projects
	           LEFT OUTER JOIN permissions_project_user
	           ON project_id = project_user_key_project_id
	           WHERE project_id='".$aFormValues['project_id']."'
	           ";
		$res = $this->ccdSQL->query( $query );
		$project = $this->ccdSQL->fetch_assoc( $res );
		$this->smarty->assign( "project", $project );
		$navitree = $this->tools->getNaviTree( 0, "", $aFormValues['project_id'] );
		$this->smarty->assign( "navitree", $navitree );

		$languages = array ();
		$query = "SELECT * FROM languages LEFT OUTER JOIN
	           mn_project_language
	           ON (project_language_key_language_id=lang_id AND project_language_key_project_id='".$aFormValues['project_id']."')
	           WHERE lang_active=1
	           order by lang_name";
		$res = $this->ccdSQL->query( $query );

		while ( $row = $this->ccdSQL->fetch_assoc( $res ) )
			$languages[] = $row;
		$this->smarty->assign( "languages", $languages );

		$query = "SELECT * FROM project_domains WHERE project_domain_id='".$aFormValues['pdomain_id']."'";
		$res = $this->ccdSQL->query( $query );
		$project_domain = $this->ccdSQL->fetch_assoc( $res );

		$this->smarty->assign( "project_domain", $project_domain );
		$sHtml = $this->smarty->fetch( $this->tpl_dir."project_domain_edit.tpl" );
		$objResponse->assign( "DomainEditDiv_".$aFormValues['project_id'], "innerHTML", $sHtml );

		$objResponse->script("$('#DomainEditDiv_".$aFormValues['project_id']."').slideDown('slow');");
		return $objResponse;
	}
	/* ---------------------------------------------------------------------------------------------------------------------------------- */

	function ProjectEditSave( $aFormValues )
	{

		$objResponse = new ccdsResponse();

		//$lifetime = mktime( $aFormValues['project_cache_lifetime_hour'], $aFormValues['project_cache_lifetime_min'], 1, 1, $aFormValues['project_cache_lifetime_days'], 1970 );
		$lifetime = strtotime('1.1.1970 '.($aFormValues['project_cache_lifetime_hour']+1).':'.$aFormValues['project_cache_lifetime_min'].' +'.$aFormValues['project_cache_lifetime_days'].'days');
		$this->ccdSQL->query( "UPDATE projects
	               SET project_name='".addslashes( $aFormValues['project_name'] )."',
	               project_caching='".$aFormValues['project_caching']."',
	               project_cache_lifetime='".$lifetime."',
	               project_structurecache='".$aFormValues['project_structurecache']."',
	               project_force_compile='".$aFormValues['project_force_compile']."'
	
	               WHERE project_id='".$aFormValues['project_id']."'" );

		$this->ccdSQL->query( "DELETE FROM mn_project_language WHERE project_language_key_project_id='".$aFormValues['project_id']."'" );

		if ( is_array( $aFormValues['project_domain_key_lang_id'] ) )
		{
			foreach ( $aFormValues['project_domain_key_lang_id'] as $lang_id )
			{
				$this->ccdSQL->query( "INSERT INTO mn_project_language VALUES ('".$aFormValues['project_id']."','".$lang_id."')" );
			}
		}
		$objResponse->loadClassCommand('ccdsDefaultPlugin','UpdateProjectLanguages',(array("project_id" => $aFormValues['project_id'])) );
		$objResponse->loadcommands( $this->ProjectEdit( array ("project_id" => $aFormValues['project_id'])) );
		return $objResponse;
	}

	/* ---------------------------------------------------------------------------------------------------------------------------------- */

	function ProjectDomainEditSave( $aFormValues )
	{

		$objResponse = new ccdsResponse();

		if ( $aFormValues['project_domain_id'] != "" )
		{
			if ( $aFormValues['del_project_domain'] != 1 )
			{



				if ('' == $aFormValues['project_domain_name']) 
				{
					$objResponse->assign('edit_project_domain_name_error_'.$aFormValues['project_id'],'innerHTML','Bitte geben Sie eine Domain ein');
					return $objResponse;
				}
		
				$objResponse->clear('edit_project_domain_name_error_'.$aFormValues['project_id'],'innerHTML');

				$query = "UPDATE project_domains
	                   SET
	                   project_domain_name='".$aFormValues['project_domain_name']."',
	                   project_domain_key_lang_id='".$aFormValues['project_domain_key_lang_id']."',
	                   project_domain_key_obj_id='".$aFormValues['project_domain_key_obj_id']."'
	                   WHERE project_domain_id='".$aFormValues['project_domain_id']."'
	
	                   ";
				$this->ccdSQL->query( $query );

			}else
			{
				$query = " DELETE FROM project_domains WHERE project_domain_id='".$aFormValues['project_domain_id']."'	";
				$this->ccdSQL->query( $query );
			}
		}

		$objResponse->script( 'new Effect.Highlight("Content",{startcolor:"#D6EAFB",endcolor:"#ffffff",restorecolor:"#ffffff"});' );
		$objResponse->loadcommands( $this->ProjectDomains( array ("project_id" => $aFormValues['project_id'])) );
		return $objResponse;
	}

	/* ---------------------------------------------------------------------------------------------------------------------------------- */

	function ProjectDomainNewSave( $aFormValues )
	{

		$objResponse = new ccdsResponse();
		
		if ('' == $aFormValues['project_domain_name']) 
		{
			$objResponse->assign('project_domain_name_error_'.$aFormValues['project_id'],'innerHTML','Bitte geben Sie eine Domain ein');
			return $objResponse;
		}

		$objResponse->clear('project_domain_name_error_'.$aFormValues['project_id'],'innerHTML');
		
		$this->ccdSQL->query( "INSERT INTO project_domains
	              (
		              project_domain_name,
		              project_domain_key_lang_id,
		              project_domain_key_obj_id,
		              project_domain_key_project_id
	              )
	              VALUES (
	              '".addslashes($aFormValues['project_domain_name'])."',
	              '".addslashes($aFormValues['project_domain_key_lang_id'])."',
	              '".addslashes($aFormValues['project_domain_key_obj_id'])."',
	              '".addslashes($aFormValues['project_id'])."'
	              )
	              " );
		$objResponse->loadcommands( $this->ProjectDomains( array ("project_id" => $aFormValues['project_id'])) );
		$objResponse->script( 'new Effect.Highlight("Content",{startcolor:"#D6EAFB",endcolor:"#ffffff",restorecolor:"#ffffff"});' );
		return $objResponse;
	}
	/* ---------------------------------------------------------------------------------------------------------------------------------- */
	/* ---------------------------------------------------------------------------------------------------------------------------------- */

	function ProjectRootAdd( $aFormValues )
	{

		$objResponse = new ccdsResponse();
		$query = "SELECT * FROM obj_index WHERE obj_parent_obj_id='0' AND obj_key_project_id='".$aFormValues['project_id']."' order by obj_position desc LIMIT 1";
		$res = $this->ccdSQL->query( $query );
		$pos = $this->ccdSQL->fetch_assoc( $res );

		$query = "SELECT * FROM classes LEFT JOIN extensions ON class_key_ext_id = ext_id WHERE ext_key_project_id='".$aFormValues['project_id']."' AND class_name='ROOT'";
		$res = $this->ccdSQL->query( $query );

		if ( $class = $this->ccdSQL->fetch_assoc( $res ) )
		{

			$query = "SELECT * FROM templates LEFT JOIN mn_template_class ON (tpl_class_key_tpl_id=tpl_id AND tpl_class_key_class_id='".$class['class_id']."') WHERE 1 ORDER BY tpl_class_default desc LIMIT 1";
			$res = $this->ccdSQL->query( $query );
			$tpl = $this->ccdSQL->fetch_assoc( $res );

			$query = "INSERT INTO obj_index
	               (
	               obj_parent_obj_id,
	               obj_key_tpl_id,
	               obj_created,
	               obj_created_key_user_id,
	               obj_key_class_id,
	               obj_position,
	               obj_key_project_id
	               )
	               VALUES (
	               '0',
	               '".$tpl['tpl_id']."',
	               '".time()."',
	               '".$_SESSION['CMS']['user_id']."',
	               '".$class['class_id']."',
	               '".( $pos['obj_position'] + 1 )."',
	               '".$aFormValues['project_id']."'
	               )
	               ";
			$res = $this->ccdSQL->query( $query );
			$new_obj_id = $this->ccdSQL->insert_id();

			$query = "SELECT * FROM languages LEFT JOIN
	               mn_project_language
	               ON (project_language_key_language_id=lang_id AND project_language_key_project_id='".$aFormValues['project_id']."')
	
	
	               order by lang_name";
			$res = $this->ccdSQL->query( $query );

			while ( $lang = $this->ccdSQL->fetch_assoc( $res ) )
			{

				$query = "INSERT INTO obj_names
	                   (
	                   obj_name_key_obj_id,
	                   obj_name_key_lang_id,
	                   obj_name_name,
	                   obj_name_serialized
	
	                   )
	                   VALUES (
	                   '".$new_obj_id."',
	                   '".$lang['lang_id']."',
	                   '".addslashes( $aFormValues['obj_name'] )."',
	                   '".$this->tools->serialize_name( $aFormValues['obj_name_name'], 0, $lang['lang_id'], $new_obj_id )."'
	                   )
	                   ";
				$this->ccdSQL->query( $query );
			}
			$objResponse->loadClassCommand( "ccdsDefaultPlugin", "updateProjectNavi",array("project_id" => $aFormValues['project_id']) );
		}else
		{
			$objResponse->alert( $objResponse, "Es ist keine ROOT-Klasse definiert!" );
		}
		return $objResponse;
	}
	/* ---------------------------------------------------------------------------------------------------------------------------------- */

	function create_table_from_conf( $filename, $class_dir )
	{
		if ( file_exists( $filename ) )
		{

			$xml = new ccdsXMLparser( $filename );
			$config = $xml->getArray();
			if ( is_array( $config ) )
				if ( is_array( $config[$class_dir]['tables'] ) )
				{
					$primary = "";
					$query = "";
					$seperator = "";

					foreach ( $config[$class_dir]['tables']['columns'] as $fieldname => $values )
					{
						$fieldtype = "";

						switch ( $values['type'] )
						{
							case "bigint":
								if ( $values['length'] == "unsigned" )
								{
									$fieldtype = "bigint(20) unsigned NOT NULL default '0' ";
								}
								else
								{
									$fieldtype = "bigint(".$values['length'].") NOT NULL default '0' ";
								}
								break;

							case "int":
								if ( $values['length'] == "unsigned" )
								{
									$fieldtype = "int(10) unsigned NOT NULL default '0' ";
								}
								else
								{
									$fieldtype = "int(".$values['length'].") NOT NULL default '0' ";
								}
								break;

							case "text":
								if ( $values['length'] == "-1" )
								{
									$fieldtype = "text NOT NULL ";
								}
								else
								{
									$fieldtype = "text (".$values['length'].") NOT NULL ";
								}
								break;

							case "date":
								$fieldtype = " date ";

							case "datetime":
								$fieldtype = " datetime ";
								break;

							case "varchar":
								if ( $values['length'] == "-1" )
								{
									$fieldtype = "varchar(255) NOT NULL default '' ";
								}
								else
								{
									$fieldtype = "varchar (".$values['length'].") NOT NULL default '' ";
								}
								break;
						}

						if ( $values['label'] == "id" )
						{
							$primary = ", PRIMARY KEY  (".$fieldname.")";
						}
						$query .= $seperator.$fieldname." ".$fieldtype;
						$seperator = ",\n ";
					}
					$query = "CREATE TABLE IF NOT EXISTS ".$config[$class_dir]['tables']['name']." \n ( ".$query." ".$primary.") ENGINE=MyISAM DEFAULT CHARSET=utf8;";
					;
					$this->ccdSQL->query( $query );
				}
		}
		return $this->ccdSQL->error();
	}
// -------------------------------------------------------------------------------------------------------------------

/* ---------------------------------------------------------------------------------------------------------------------------------- */

}



$ccdsModuleManager = ccdsModuleManager::getInstance();
$ccdsModuleManager->register('admin_navi','ccdsProject','getAdminNavi');
?>